class Admin::ToursController < ApplicationController
  layout 'admin/application'
  # GET /tours
  # GET /tours.xml
  def index
    @tours = Tour.find(:all)
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @tours }
    end
  end
  
  # GET /tours/1
  # GET /tours/1.xml
  def show
    @tour = Tour.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @tour }
    end
  end
  
  # GET /tours/new
  # GET /tours/new.xml
  def new
    @tour = Tour.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @tour }
    end
  end
  
  # GET /tours/1/edit
  def edit
    @tour = Tour.find(params[:id])
  end
  
  # POST /tours
  # POST /tours.xml
  def create
    @tour = Tour.new(params[:tour])
    # 只有上传文件的时候，才需要进行解析
    if params[:picture].class == ActionController::UploadedStringIO ||
      params[:picture].class == ActionController::UploadedTempfile
      pic = Attachment.new
      pic.image_only = true
      pic.load_from_file_field(params[:picture])
      @tour.picture = pic
    end
    
    respond_to do |format|
      if @tour.save
        flash[:notice] = 'Tour was successfully created.'
        format.html { redirect_to([:admin, @tour]) }
        format.xml  { render :xml => @tour, :status => :created, :location => @tour }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @tour.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # PUT /tours/1
  # PUT /tours/1.xml
  def update
    @tour = Tour.find(params[:id])
    puts params[:picture].class
    # 只有上传文件的时候，才需要进行解析
    if params[:picture].class == ActionController::UploadedStringIO ||
      params[:picture].class == ActionController::UploadedTempfile
      pic = Attachment.new
      pic.image_only = true
      pic.load_from_file_field(params[:picture])
      @tour.picture = pic
      
    end
    
    respond_to do |format|
      if @tour.update_attributes(params[:tour])
        flash[:notice] = 'Tour was successfully updated.'
        format.html { redirect_to([:admin, @tour]) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @tour.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /tours/1
  # DELETE /tours/1.xml
  def destroy
    @tour = Tour.find(params[:id])
    @tour.destroy
    
    respond_to do |format|
      format.html { redirect_to(admin_tours_url) }
      format.xml  { head :ok }
    end
  end
end
